<?php

class Orderable extends AppModel {

	var $name = "Orderable";
	
	//var $primaryKey = 'id';
	
	var $belongsTo = array(
		'Delivery'=>array('foreignKey' => "delivery_id"),
		'Product'=>array('foreignKey' => "product_id")
	);
	
	var $hasMany = array(
		"BasketItem" => array('order' => 'member_id'));

	var $recursive = 2;
	
	function c_find($where = '1', $order = null, $limit = null, $page = 1) {
		if ($order === null) {
			$order = 'Product.product_name ASC';
		}
		$sql = 'SELECT Orderable.id, Product.product_id, Product.producer_id, Product.product_name , Member.business_name, Member.email_address , Orderable.price , Orderable.priced_by , Orderable.ordered_by, Orderable.inventory_start, Orderable.inventory_left , Product.inventory_on, Product.maximum_weight , Product.minimum_weight , Product.random_weight , Product.tax_exempt, Subcategory.subcategory_id, Category.category_id, Category.category_name, Subcategory.subcategory_name, Product.detailed_notes'
 			.' FROM members as Member , producers as Producer , orderables as Orderable, product_list as Product'
 			.' LEFT JOIN subcategories AS Subcategory ON Product.subcategory_id = Subcategory.subcategory_id'
 			.' LEFT JOIN categories AS Category ON Subcategory.category_id = Category.category_id'
 			.' WHERE Product.product_id = Orderable.product_id '
 			.' AND Producer.producer_id = Product.producer_id '
 			.' AND Member.member_id = Producer.member_id '
 			.' AND ' . $where
 			.' ORDER BY '.$order . $limit;
 		return $this->query($sql);
	}
	
	function addForProduct($product, $del = null) {
		
		$ret = 0;
		
		if (!empty($product)) {
			$orderable = array('Orderable' => array());
			
			if (is_null($del)) {
				App::import('Model', 'Delivery');
				$d = new Delivery();
				$del = $d->currentId();
			}
			
			$o = new Orderable();
			
			$orderable['Orderable']['delivery_id'] = $del;
			$orderable['Orderable']['product_id'] = $product['Product']['product_id'];
			$orderable['Orderable']['price'] = $product['Product']['unit_price'];
			$orderable['Orderable']['ordered_by'] = $product['Product']['ordering_unit'];
			$orderable['Orderable']['priced_by'] = $product['Product']['pricing_unit'];
			$orderable['Orderable']['inventory_start'] = $product['Product']['inventory'];
			$orderable['Orderable']['inventory_left'] = $product['Product']['inventory'];
			$o->save($orderable);
			
			$ret = 1;
		}
		
		
		return $ret;
		
	}
	
	function initialize() {
		
		$ds = date('Y-m-d H:i:s');
		$del = $this->Delivery->find("order_open < '" . $ds . "' AND '" . $ds . "' < order_closed");
		$delivery_id = $del['Delivery']['delivery_id'];
		$message = '';
		if (empty($del)) {
			$message = "We are not in an order cycle right now.";
		} else if ($del['Delivery']['open']) {
			$message = "Order Session is already open and setup.";
		} else {
			//get current products

			$prods = $this->Product->find('all', array('conditions' => "Product.donotlist = '0' and Producer.donotlist_producer = '0'", 'recursive' => 0));
		
			//save orderables
			$p = 0;
			foreach ($prods as $product) {
				$this->addForProduct($product, $delivery_id);
				$this->Orderable->id = null;
				$p++;
			}
			$message = "Order Session Open: " . $p . " products transferred.";
			$this->Delivery->id = $delivery_id;
			$this->Delivery->saveField('open',1);
		}
		return $message;
	}
	
}

?>
